package com.tgy.core.service.impl;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tgy.core.mapper.SysAdminMapper;
import com.tgy.core.service.SysAdminService;
import com.tgy.enums.YesOrNo;
import com.tgy.page.PageQuery;
import com.tgy.page.TableDataInfo;
import com.tgy.pojo.SysAdmin;
import com.tgy.result.LayuiTableResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
 * 系统用户信息Service业务层处理
 *
 * @author 自动生成
 * @date 2022-06-20
 */
@Service
public class SysAdminServiceImpl extends ServiceImpl<SysAdminMapper, SysAdmin> implements SysAdminService {

    @Resource
    private SysAdminMapper baseMapper;

    /**
     * 查询系统用户信息
     *
     * @param id 系统用户信息主键
     * @return 系统用户信息
     */
    @Override
    public SysAdmin queryById(String id){
        return baseMapper.selectById(id);
    }

    /**
     * 查询系统用户信息列表
     *
     * @param bo 系统用户信息
     * @return 系统用户信息
     */
    @Override
    public TableDataInfo<SysAdmin> queryPageList(SysAdmin bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysAdmin> lqw = buildQueryWrapper(bo);
        Page<SysAdmin> result = baseMapper.selectPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public LayuiTableResult queryLayuiPageList(SysAdmin bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysAdmin> lqw = buildQueryWrapper(bo);
        Page<SysAdmin> result = baseMapper.selectPage(pageQuery.build(), lqw);
        return new LayuiTableResult(result.getTotal(),result.getRecords());
    }

    /**
     * 查询系统用户信息列表
     *
     * @param bo 系统用户信息
     * @return 系统用户信息
     */
    @Override
    public List<SysAdmin> queryList(SysAdmin bo) {
        LambdaQueryWrapper<SysAdmin> lqw = buildQueryWrapper(bo);
        return baseMapper.selectList(lqw);
    }

    private LambdaQueryWrapper<SysAdmin> buildQueryWrapper(SysAdmin bo) {
        LambdaQueryWrapper<SysAdmin> lqw = Wrappers.lambdaQuery();
        lqw.eq(StrUtil.isNotBlank(bo.getAccount()),SysAdmin::getAccount,bo.getAccount());
        lqw.like(StrUtil.isNotBlank(bo.getUserName()),SysAdmin::getUserName,bo.getUserName());
        lqw.eq(Objects.nonNull(bo.getSex()),SysAdmin::getSex,bo.getSex());
        if (Objects.nonNull(bo.getStatus()) && bo.getStatus() == -1){
            lqw.eq(SysAdmin::getStatus, YesOrNo.YES.type);
        }else if (Objects.nonNull(bo.getStatus())){
            lqw.eq(Objects.nonNull(bo.getStatus()),SysAdmin::getStatus,bo.getStatus());
        }
        return lqw;
    }

    /**
     * 新增系统用户信息
     *
     * @param bo 系统用户信息
     * @return 结果
     */
    @Override
    public Boolean insertByBo(SysAdmin bo) {
        // if (validEntityBeforeSave(bo)){
            boolean flag = baseMapper.insert(bo) > 0;
            if (flag) {
                bo.setId(bo.getId());
            }
            return flag;
        // }else {
        //     GlobalException.display(GraceJSONResult.error("该账号已存在"));
        // }
        // return false;
    }

    private Boolean validEntityBeforeSave(SysAdmin bo) {
        SysAdmin sysAdmin = new SysAdmin();
        sysAdmin.setAccount(bo.getAccount());
        List<SysAdmin> sysAdmins = this.queryList(sysAdmin);
        return !CollectionUtil.isNotEmpty(sysAdmins);
    }

    /**
     * 修改系统用户信息
     *
     * @param bo 系统用户信息
     * @return 结果
     */
    @Override
    public Boolean updateByBo(SysAdmin bo) {
        // PBizPatient update = BeanUtil.toBean(bo, PBizPatient.class);
        // validEntityBeforeSave(SysAdmin);
        /*if (validEntityBeforeSave(bo)){
            return baseMapper.updateById(bo) > 0;
        }else {
            GlobalException.display(GraceJSONResult.error("该账号已存在"));
        }
        return false;*/
        return baseMapper.updateById(bo) > 0;
    }

    @Override
    public Boolean deleteById(String id) {
        return baseMapper.deleteById(id) > 0;
    }


    /**
     * 批量删除系统用户信息
     *
     * @param ids 需要删除的系统用户信息主键
     * @return 结果
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 根据Id修改用户状态
     */
    @Override
    public int updateStatus(String id, Integer status) {
        return baseMapper.updateStatus(id,status);
    }



}
